<?php

namespace RedMVC;

/**
 * ViewHelper class
 *  
 * @category    RedMVC
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class ViewHelper{

    protected $request;
    protected $db;
    protected $view;
    protected $controller;

    public function setRequest($request){
        $this->request = $request;
    }
    
    public function setDb($db){
        $this->db = $db;
    }
    
    public function setView($view){
        $this->view = $view;
    }
    
    public function setController($controller){
        $this->controller = $controller;
    }

    public function __call($method, $args){
        $className = ucfirst($method);    
        $viewHelper = APP_PATH.DS.'views'.DS.'helpers'.DS.$className.'.php';    
        if(file_exists($viewHelper)){
            $className = "views\\helpers\\".$className;
            $viewHelperClass = new $className();
            if(get_parent_class($viewHelperClass) == 'RedMVC\ViewHelper'){
                $viewHelperClass->setRequest($this->request);
                $viewHelperClass->setDb($this->db);
                $viewHelperClass->setView($this->view);
                $viewHelperClass->setController($this->controller);
                
                //return call_user_func_array(array($viewHelperClass, $method), $args);

                if(method_exists($viewHelperClass, 'invoke')){
                    return call_user_func_array(array($viewHelperClass, 'invoke'), $args);
                }
                else{
                    return 'Method "invoke" does not exist in "'.$className.'"';
                }

                //$reflectionMethod = new \ReflectionMethod($className, $method);
                //return $reflectionMethod->invokeArgs($viewHelperClass, $args);
            }
        }
        else{
            //throw new \Exception('View Helper '.$method.' doesn\'t exist.');
            return 'ViewHelper "'.$method.'" doesn\'t exist.';
        }        
    }
    
    // ready viewHelpers
    
    public function baseUrl($filePath = null){
        if(null === $filePath){        
            return $this->request->getBaseUrl();
        }
        return $this->request->getBaseUrl().$filePath;
    }

    public function rootUrl($filePath = null){
        if(null === $filePath){
            return $this->request->getRootUrl();
        }
        return $this->request->getRootUrl().$filePath;
    }
    
    public function url($controller, $action, $params = array(), $absoluteUrl = false){
        return Controller\Front::url($controller, $action, $params, $absoluteUrl);
    }
    
    public function partial($viewScriptPath, array $vars){
        $view = new View(APP_PATH.DS.'views'.DS.'scripts'.DS.$viewScriptPath);
        $view->assignArray($vars);
        return $view->render();
    }
    
    public function translate($str){
        $translate = Registry::get('RedMVC_Translate');
        return $translate->_($str);
    }

    

}